﻿using IBll;
using IBll.DTO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;

namespace webApi0205.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class StudentController : ControllerBase
    {
        public StudentController(IStudentBll bll,IWebHostEnvironment web)
        {
            Bll = bll;
            Web = web;
        }

        public IStudentBll Bll { get; }
        public IWebHostEnvironment Web { get; }

        [HttpPost]
        public async Task<int> Add(Student student)
        {
            return await Bll.AddAsync(student);
        }
        [HttpGet]
        public PageResultDTo<Student> GetStudentPage(int index, int size, string? name)
        {
            return Bll.GetStudentPage(index, size, name);
        }
        [HttpDelete]
        public Task<int> Delete(string ids)
        {
            return Bll.RangeDeleteAsync(ids);
        }
        [HttpGet]
        public Student GetStudent(int id)
        {
            return Bll.GetSingle(id);
        }
        [HttpPut]
        public Task<int> Update(Student student)
        {
            return Bll.UpdateAsync(student);
        }
        [HttpPost]
        public string Upfile(IFormFile file)
        {
            var id =Guid.NewGuid().ToString();
            var path=Web.WebRootPath+@"\pic\" + id+file.FileName;
            using (FileStream fs = System.IO.File.Create(path))
            {
                file.CopyTo(fs);
                fs.Close();
            }
            return "/pic/"+id+file.FileName;
        }
    }
}
